package com.example.selenium.queue.rabbitmq;

import com.example.selenium.queue.rabbitmq.config.RabbitMqConstants;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SendService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     * @param exchange
     * @param routingKey
     * @param data
     */
    public void send(String exchange,String routingKey,String data){
        CorrelationDataExt tCorrelationDataExt = new CorrelationDataExt(data);
        rabbitTemplate.convertAndSend(exchange,routingKey, tCorrelationDataExt.getMessageInfo(),tCorrelationDataExt );
    }

    /**
     * @param exchange
     * @param routingKey
     * @param waybillNo 运单号
     * @param data
     */
    public void send(String exchange,String routingKey,String waybillNo,String data){
        CorrelationDataExt tCorrelationDataExt = new CorrelationDataExt(waybillNo,data);
        rabbitTemplate.convertAndSend(exchange,routingKey, tCorrelationDataExt.getMessageInfo(),tCorrelationDataExt );
    }

    /**
     * 发送消息之扇形
     */
    public void sendByFanout(){
        String msg="发送扇形消息";
        rabbitTemplate.convertAndSend("fanoutExchange", null, msg);
    }

    /**
     * 发送主题消息
     * @return
     */
    public String sendByTopic() {
        String msg = "hello topic";
        rabbitTemplate.convertAndSend("myTopicExchange", "topic.01", msg + " topic.01");
        rabbitTemplate.convertAndSend("myTopicExchange", "topic.xxx", msg + " topic.xxx");
        return "success";
    }
}
